package com.cybertooth.dbconvenience.query.columns;

import com.cybertooth.dbconvenience.query.enums.AggregateFunction;
import com.google.inject.Inject;

/**
 * <p></p>
 *
 * @author dnelson
 */
public class AggregateColumn extends ColumnWithAs {

	// -----------------------------------------------------------------------Fields

	protected AggregateFunction function;

	// -----------------------------------------------------------------Constructors

	public static AggregateColumn column(AggregateFunction aggregateFunction, String prefix, String name) {
		// TODO: use injection
		AggregateColumn instance = new AggregateColumn(new UnderscoreToCamelCase());
		instance.function = aggregateFunction;
		instance.column = new Column(prefix, name);
		return instance;
	}

	public static AggregateColumn column(AggregateFunction aggregateFunction, String name) {
		// TODO: use injection
		AggregateColumn instance = new AggregateColumn(new UnderscoreToCamelCase());
		instance.function = aggregateFunction;
		instance.column = new Column(name);
		return instance;
	}

	/**
	 * Even though this is a public constructor, you MUST use the static sugar calls as they will be using injection to
	 * provide the {@link ColumnNameTransformer}.  <em>There is no public access to the the {@link #column} field which
	 * renders this constructor useless to most applications (testing excluded).</em>
	 *
	 * @param asClauseGenerator the injected {@link ColumnNameTransformer} instance.
	 * @see #column(String)
	 * @see #column(String, String)
	 */
	@Inject
	public AggregateColumn(ColumnNameTransformer asClauseGenerator) {
		super(asClauseGenerator);
	}

	// ---------------------------------------------------------------Public Methods

	@Override
	public String toSql() {
		return new StringBuilder()
				.append(function.toSql()).append("(").append(super.column.toSql().trim()).append(")")
				.append(" AS ").append(super.asClauseGenerator.transform(super.column.getName()))
				.toString();
	}
}
